package tech.topcoder.springcloud.simpleservice.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Mybatis 对象生成
 * Created by hhj on 16-12-12.
 */
public class MybatisGenerator {

    private static String daoDir = "src/main/java";
    private static String daoPackage = "tech.topcoder.springcloud.simpleservice.dao";

    private static String mapperDir = "src/main/resources/META-INF/db";
    private static String mapperPackage = "mapper";

    private static String beanDir = "src/main/java/";
    private static String beanPackage = "tech.topcoder.springcloud.simpleservice.bean";   // java bean的包路径
    private static String rootClass = "tech.topcoder.springcloud.simpleservice.bean.BaseBean";   // bean 继承的类

    private static String driverClassName = "com.mysql.jdbc.Driver";
    private static String connectionURL = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8";
    private static String dbUser = "root";
    private static String dbPassword = "111111";

    private static String targetRuntime = CustomMyBatis3Impl.class.getName();
    private static String pagePlugin = PaginationPlugin.class.getName();

    /**
     * 分页类名
     */
    public static String PAGE_JAVA_TYPE = Page.class.getName();


    public static void main(String[] args) throws Exception {
        URL path = MybatisGenerator.class.getClassLoader().getResource("");
        String rootPath = path.getPath() + "../../";

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;

        // 数据库连接
        JDBCConnectionConfiguration jdbcConn = new JDBCConnectionConfiguration();
        jdbcConn.setDriverClass(driverClassName);
        jdbcConn.setConnectionURL(connectionURL);
        jdbcConn.setUserId(dbUser);
        jdbcConn.setPassword(dbPassword);

        // 注释
        CommentGeneratorConfiguration commentCfg = new CommentGeneratorConfiguration();
        commentCfg.addProperty("suppressDate", "true");
        commentCfg.addProperty("suppressAllComments", "true");

        // Bean包
        JavaModelGeneratorConfiguration javaModelCfg = new JavaModelGeneratorConfiguration();
        javaModelCfg.setTargetPackage(beanPackage);
        javaModelCfg.setTargetProject(rootPath + beanDir);
        javaModelCfg.addProperty("enableSubPackages", "true");
        javaModelCfg.addProperty("trimStrings", "true");
        javaModelCfg.addProperty("rootClass", rootClass);

        // Mapper包
        SqlMapGeneratorConfiguration sqlMapCfg = new SqlMapGeneratorConfiguration();
        sqlMapCfg.setTargetPackage(mapperPackage);
        sqlMapCfg.setTargetProject(rootPath + mapperDir);
        sqlMapCfg.addProperty("enableSubPackages", "true");

        // 生成Dao类
        JavaClientGeneratorConfiguration clientCfg = new JavaClientGeneratorConfiguration();
        clientCfg.setConfigurationType("XMLMAPPER");
        clientCfg.setTargetPackage(daoPackage);
        clientCfg.setTargetProject(rootPath + daoDir);
        clientCfg.addProperty("enableSubPackages", "true");

        // 配置
        Context context = new Context(ModelType.CONDITIONAL);
        context.setId("springboot-demo");
//        context.setTargetRuntime("MyBatis3");
        context.setTargetRuntime(targetRuntime);
        context.setCommentGeneratorConfiguration(commentCfg);
        context.setJdbcConnectionConfiguration(jdbcConn);
        context.setJavaModelGeneratorConfiguration(javaModelCfg);
        context.setSqlMapGeneratorConfiguration(sqlMapCfg);
        context.setJavaClientGeneratorConfiguration(clientCfg);

        // 分页插件
        PluginConfiguration pluginCfg = new PluginConfiguration();
        pluginCfg.setConfigurationType(pagePlugin);
        context.addPluginConfiguration(pluginCfg);

        // 表
        TableConfiguration tblCfg = new TableConfiguration(context);
        tblCfg.setTableName("%");
        // tblCfg.setCountByExampleStatementEnabled(false);
        tblCfg.setUpdateByExampleStatementEnabled(false);
        tblCfg.setDeleteByExampleStatementEnabled(false);
        //tblCfg.setSelectByExampleStatementEnabled(false);
        GeneratedKey gk = new GeneratedKey("id", "JDBC", true, "post");
        tblCfg.setGeneratedKey(gk);

        context.addTableConfiguration(tblCfg);

        /*TableConfiguration tblCfg2 = new TableConfiguration(context);
        BeanUtils.copyProperties(tblCfg, tblCfg2);
        tblCfg2.setTableName("member");
        context.addTableConfiguration(tblCfg2);*/

        Configuration config = new Configuration();
        config.addContext(context);

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);

        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

        for (String warning : warnings) {
            System.out.println(warning);
        }

        System.out.println(rootPath);
    }
}
